#!/usr/bin/env bash
source ./config/env.sh
clear


mkdir -p ${K8S_CA_DIR}/kube-proxy

cat > ${K8S_CA_DIR}/kube-proxy/kube-proxy-csr.json <<EOF
{
  "CN": "system:kube-proxy",
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "4Paradigm"
    }
  ]
}
EOF

cd ${K8S_CA_DIR}/kube-proxy

${K8S_BIN_DIR}/cfssl gencert -ca=${K8S_CA_DIR}/admin/ca.pem \
  -ca-key=${K8S_CA_DIR}/admin/ca-key.pem \
  -config=${K8S_CA_DIR}/admin/ca-config.json \
  -profile=kubernetes  kube-proxy-csr.json | cfssljson -bare kube-proxy


for node_ip in ${NODE_IP}
do
    ssh root@${node_ip} "test -e ${K8S_CA_DIR}/kube-proxy || mkdir -p ${K8S_CA_DIR}/kube-proxy"
    scp ${K8S_CA_DIR}/kube-proxy/* root@${node_ip}:${K8S_CA_DIR}/kube-proxy
done

${K8S_BIN_DIR}/kubectl config set-cluster kubernetes \
  --certificate-authority=${K8S_CA_DIR}/admin/ca.pem \
  --embed-certs=true \
  --server=${APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

${K8S_BIN_DIR}/kubectl config set-credentials kube-proxy \
  --client-certificate=${K8S_CA_DIR}/kube-proxy/kube-proxy.pem \
  --client-key=${K8S_CA_DIR}/kube-proxy/kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=kube-proxy.kubeconfig

${K8S_BIN_DIR}/kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=kube-proxy.kubeconfig

${K8S_BIN_DIR}/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

for node_hostname in ${NODE_HOSTNAME}
do
    echo "=========================================== ${node_hostname} ==========================================="
    scp -o "StrictHostKeyChecking no" ${K8S_CA_DIR}/kube-proxy/kube-proxy.kubeconfig root@${node_hostname}:${K8S_CA_DIR}/kube-proxy
    ssh root@${node_hostname} "mkdir -p ${K8S_DATA}/config/kube-proxy/yaml/"
done

mkdir -p ${K8S_DATA}/config/kube-proxy/yaml/

cat > ${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-config.yaml.template << EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
clientConnection:
  kubeconfig: "${K8S_CA_DIR}/kube-proxy/kube-proxy.kubeconfig"
bindAddress: ##NODE_IP##
clusterCIDR: ${CLUSTER_CIDR}
healthzBindAddress: ##NODE_IP##:10256
hostnameOverride: ##NODE_NAME##
metricsBindAddress: ##NODE_IP##:10249
mode: "ipvs"
EOF

for (( i=1; i <= ${NODE}; i++ ))
do
    sed -e "s/##NODE_NAME##/`echo ${NODE_HOSTNAME} | cut -d " " -f $i`/" -e "s/##NODE_IP##/`echo ${NODE_IP} | cut -d " " -f $i`/" ${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-config.yaml.template > ${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-`echo ${NODE_HOSTNAME} | cut -d " " -f $i`-config.yaml
    scp ${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-`echo ${NODE_HOSTNAME} | cut -d " " -f $i`-config.yaml root@`echo ${NODE_HOSTNAME} | cut -d " " -f $i`:${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-config.yaml
done


cat > ${K8S_DATA}/config/kube-proxy/kube-proxy.service <<EOF
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
WorkingDirectory=${K8S_DATA}/data/kube-proxy
ExecStart=${K8S_BIN_DIR}/kube-proxy \\
  --config=${K8S_DATA}/config/kube-proxy/yaml/kube-proxy-config.yaml \\
  --logtostderr=true \\
  --v=2
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    scp ${K8S_DATA}/config/kube-proxy/kube-proxy.service root@${node_ip}:/etc/systemd/system/
done

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "test -e ${K8S_DATA}/data/kube-proxy || mkdir -p ${K8S_DATA}/data/kube-proxy"
    ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kube-proxy && systemctl restart kube-proxy"
done

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "systemctl status kube-proxy|grep Active"
done

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "/usr/sbin/ipvsadm -ln"
done